Git Kata 1: New Local Repository

Step 1: Initialize a new repository#

Open a new terminal window. The command to edit and list files is given below.

The result will be something like this:

Changing the directory
Editing the storelist.txt file
Listing files

Commands

Command / Parameter

Description

cd ~/dk/storelist

This changes the current working directory to the dk/storelist directory.

nano storelist

This opens the storelist.txt file in the default text editor, nano.

ls

This lists the files in the dk/storelist directory.

The first command group changes to the dk/storelist directory, opens storelist.txt in the text editor, and lists the files in dk/storelist. The only file in the dk/storelist directory is storelist.txt.

Leave the text editor open. We’ll be switching back and forth between the terminal and the editor to view and make changes to storelist.txt.

The command to initiate the new repository is given below.

When we run the command above, the result will be something like this:

Initiating new repository

Command

Command / Parameter

Description

git init

This initializes a new Git repository in the current directory.

The git init command does one thing only: it initializes the repository by creating the hidden .git directory. A Git repository consists of the .git directory and the files and sub-directories of the repository directory, dk/storelist. The repository directory and its subdirectories are the working directory, also known as the working tree. Files that are tracked by the repository are stored and edited in the working directory.

The command to set email and username for the local repository is given below.

The output will be something like this:

Setting the email and username for the local repository

Command's Parameters

Command / Parameter

Description

config

This sets a configuration value that affects the behavior of the Git program.

user.email | user.name

These values set the username and email identity associated with the local repository.

The next two commands use config to set the username and email address for the local repository. These values are stored in files under the .git directory. Git associates an identity with changes as they’re saved to the version history. If no identity is set, commands to record changes, called commits, to a repository index will return an error.

The command to list files is given below.

After executing the command above, the output will be something like this:

Listing all the files in the directory and in .git

Commands

Command / Parameter

Description

ls -A

This lists all the files, including the hidden files.

ls .git

This lists the contents of the .git repository.

The ls command is used with the -A parameter to show all the files. This displays the hidden .git directory in dk/storelist. The ls .git command lists all the files in the .git directory. These files are managed by the Git program and shouldn’t be modified directly. The .git folder is the repository index. The index is a local database that stores the version history of changes made to the files in the working directory.

The command to get the repository status is given below.

The result of the command above will be something like this:

Getting the repository status

Command

Command / Parameter

Description

git status

This displays the status of the files in the working directory of the repository.

The git status command shows the status of all the files in the dk/storelist directory. The first execution shows that there’s one untracked file in the repository: storelist.txt. The git init command initializes a new repository, but that doesn’t cause any files in the working directory to be tracked.

The output of this command is explained as follows:

Output

Message

Meaning

"On branch master"

The current branch (branches will be discussed later).

"Initial commit"

No commits have been made, so the next commit will be the initial (first) commit.

"Untracked files:"

The files listed below are untracked, meaning that they won't be included in any commit.

"(use git add <file>… to include in what will be committed)"


"storelist.txt"

This is the only file in the working directory, listed as untracked.

"nothing added to commit but untracked files present (use "git add" to track)"

There are no changes to be committed, but Git reminds us that there are untracked files in the working directory.

Step 2: Stage changes to a repository#

The command to add a file to the index and track it is given below.

The output of the command above will be something like this:

Adding a file into the index and making it tracked

Command's Parameters

Command / Parameter

Description

add

This stages the changes to the repository.

storelist.txt

This is the file to be staged.

The git add command is used to stage changes in a repository. Changes to a repository that are staged will be part of the next commit to that repository. Git uses commits to save changes to the index. The list of commits, called the commit history, is the chronologically ordered version history of a Git repository. Commits are also used to push version history to a remote Git server.

The git add command stages the storelist.txt file. Files in a repository have a status of “tracked” or “untracked.” Prior to this command, storelist.txt was untracked, as we saw in the previous git status command. Running git add storelist.txt changes the status of storelist.txt to tracked.

The command to get the repository status is given below.

The output will be something like this:

Getting the repository status

Command's Parameter

Command / Parameter

Description

status

This displays the status of the files in the working directory of the repository.

The git status command is used to view the status of all the files in a repository. The output shows that the current branch is primary.

The output of this command is explained as follows:

Output

Message

Meaning

"On branch master"

The current branch is master(also being referred to as main branch in this chapter).

"Initial commit"

No commits have been made, so the next commit will be the initial (first) commit.

"Changes to be committed:"

The changes listed below will be included in the next commit. The git rm command can be used to unstage the change so that it won't be included in the next commit.

"(use “git rm --cached <file>...” to unstage)"


"new file: storelist.txt"

The addition of the storelist.txt file is the change that will be included in the next commit.

Step 3: Commit changes to a repository#

The command to commit the changes to the repository is given below.

Note: Typing “Initial Commit” followed by the commands “Ctrl”+“O,” “Enter,” and then “Ctrl”+“X” resembles the key combinations used in the nano text editor to save and exit a file.

The output will be something like this:

Commiting the changes to the repository

Commands

Command / Parameter

Description


commit

The git commit command creates a new commit in a repository, saving a “snapshot” of the state of the tracked files in the working directory. Executing this command with no parameters opens the default text editor (in this case, the nano command-line text editor).

Ctrl-O

This is the command to write changes to an open file.

Enter

This executes the write to the commit message.

Ctrl-X

This exits the nano editor.

This step creates a commit. A commit is a snapshot of the state of the tracked files in the working directory. The commit history of a repository is an ordered list of all commits in reverse chronological order. As new commits are added, they’re added to the top of the list, and older commits are pushed downward.

The git commit command creates the first commit in this repository. The default text editor, nano, is opened so that the commit comment may be added to the commit.

Each commit can (and should) have a comment added to it. Commenting commits is a best practice. All commits should have a brief but descriptive comment describing the changes that are included in the new commit. The first commit comment is, by convention, often “initial” or “first commit.” The commit made in this step has committed one change: the addition of the storelist.txt file.

The output of the command is:

Output

Message

Meaning


"[master (root-commit) 4fb4c55] Initial Commit"

master/main: This is the name of the current branch.

root-commit: This indicates that this is the first commit in this repository.

4fb4c55: This is the first seven characters of the commit hash.

Initial Commit: This is the commit message.

"1 file changed, 16 insertions(+)"

This is a summary of all the files that have been modified, and the insertions/deletions made to each file (in this case, only the storelist.txt file was modified).

"create mode 100644 storelist.txt"

This is a list of all files included in the commit, including a code that indicates the status of the files on the disk.

Starting with this step, each Git kata that creates a new commit will include a history visualization. As new commits are added, the visualizations will be updated to display them. The visualizations listed with the katas are displayed horizontally, which makes it easy to see a short history. Longer histories are easier to see with a vertical format. We’ll see that on the command line in a later kata. Command-line and graphical Git tools display commit histories using a variety of visual formats.

Our repository now has a single commit, represented by a circle. The first commit is referred to as the root commit. This representation also shows two labels on the commit: the branch and the HEAD reference. We’ll cover branches in the next kata.

The HEAD reference is a symbolic reference (also called a ref) to the current branch. This reference tells Git where to apply the next commit. Switching branches updates the HEAD ref to point to the current branch. Git commands such as revert and reset, which are used to reverse changes by undoing commits, also modify the HEAD ref.

The command to get the repository status is given below.

The output of the command above will be something like this:

Getting the repository status

Command's Parameter

Command / Parameter

Description

status

This displays the status of the files in the working directory of the repository.

The git status command now shows that there are no uncommitted changes in the working directory.

The output of the command is:

Output

Message

Meaning

"On branch master"

This indicates the current branch.

"nothing to commit, working tree clean"

This indicates that there are no unstaged changes in the working directory. The working directory matches the contents of the index.

Step 4: Make and commit changes to a file#

To swap lines in the file and save:

  • Open the storelist.txt file.
  • Swap the first row and the sixth row.
  • Click “Ctrl”+“S” to save.
Swapping the first and sixth rows in the file

This step will demonstrate the process of making changes to a file and then committing those changes to the repository. First, we open the storelist.txt file and make a change by swapping two lines.

The command to get the repository status is given below.

After executing the command above, the output will be something like this:

Getting the repository status

Command's Parameter

Command / Parameter

Description

status

This displays the status of the files in the working directory of the repository.

The git status command shows that storelist.txt has been modified.

The output of the command is:

Output

Message

Meaning

"On branch master"

This indicates the current branch.

"Changes not staged for commit"

This is a list of all the changes that haven't been staged. Help text indicates options for committing or discarding unstaged changes.

"modified: storelist.txt"

The one file in the repository that has been modified. If there were other modified files, they would also be listed here.

"no changes added to commit (use “git add” and/or “git commit -a”)"

This indicates that the changes listed are not staged.

The command to commit the change to the repository is given below.

The result of the command above will be something like this:

Commiting the changes to the repository

Command's Parameters

Command / Parameter

Description

commit

This creates a new commit in the repository.

-a

This stages all the modified files to the index prior to creating the commit.

-m

The -m parameter allows the commit message to be defined on the command line instead of opening a text editor.

"Reordered list"

This is the commit message. It must be enclosed in quotes.

This is the first commit we’ve made after modifying a file. The -a parameter tells Git to stage and commit all the modified and tracked files. It’s the equivalent of calling git add on every modified file, then git commit. The -m parameter allows us to include the comment on the command line. This is typically faster than opening a text editor, as we did in the previous step.

The output of the command is:

Output

Message

Meaning

"[master f8711ef] Reordered List"

This is the current branch (master/main), the commit hash, and the commit message of the new commit.

"1 file changed, 3 insertions(+), 3 deletions(-)"

This is a summary of all the changes included in the new commit.

Note: Our repository now has two commits, represented by two connected circles. A Git commit history is a graph, in which each commit refers to the previous commit. The new commit that was just added contains a reference to its parent commit, which was the first commit in the repository.

The command to get the repository status is given below.

The result of the command above will be something like this:

Getting the repository status

Command's Parameter

Command / Parameter

Description

status

This displays the status of the files in the working directory of the repository.

The git status command now shows that the working tree (another term for the working directory) is clean, which means that there are no uncommitted changes in the working directory. When changes are present and unstaged and/or uncommitted, the working directory is referred to as dirty.

The output of the command is:

Output

Message

Meaning

"On branch master"

This indicates the current branch.

"nothing to commit, working tree clean"

This indicates that there are no changes to stage or commit. The working directory matches the index.

To swap lines in the file and save:

  • Swap the third and fourth rows in storelist.txt.
  • Click “Ctrl”+“S” to save.
Swapping the third and forth rows in the file

This step makes and commits another change to storelist.txt.

The command to commit the change to the repository is given below.

The output of the command above will be something like this:

Commiting the changes to the repository

Command's Parameters

Command / Parameter

Description

commit

This creates a new commit in the repository.

-a

The -a parameter of the commit command tells Git to commit all changes to tracked files. Untracked files are not affected.

-m

The -m parameter allows the commit message to be defined on the command line instead of opening a text editor.

"Another reorder"

This is the commit message.

This step commits the second edit made to the storelist.txt file.

The output of the command is:

Output

Message

Meaning

"[master c70b36e] Another reorder"

This is the current branch, commit hash, and commit message of the new commit.

"1 file changed, 1 insertion(+), 1 deletion(-)

This is a summary of the changes included in the new commit.

Note: The repository now has three commits. The HEAD ref continues to refer to the most recent commit on the current branch.

The command to get the repository status is given below.

When we execute the command above, the output will be something like this:

Getting the repository status

Command's Parameter

Command / Parameter

Description

status

This displays the status of the files in the working directory of the repository.

The git status command now shows that the working tree is clean again after committing the second change to storelist.txt.

Practice commands#

We’ve given a terminal and a table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!

Commands

Step

Command

This changes to ~/dk/storelist/.

cd ~/dk/storelist

This edits the storelist.txt file.

nano storelist.txt

This lists all the files in dk/storelist.

ls

This initializes a new repository in dk/storelist.

git init

This sets the email address for the local repo to devops@katas.com.

git config user.email 'devops@katas.com'

This sets the username for the local repo to devops.

git config user.name 'devops'

This lists all the files in dk/storelist, including hidden.

ls -A

This lists all the files in the Git index of the new repository.

ls .git

This gets the status of the repository.

git status

This adds storelist.txt to the index and makes it tracked.

git add storelist.txt

This gets the status of the repository.

git status

This commits the changes to the repository.

git commit

Type "Initial Commit"

  • “Ctrl-O”
  • “Enter”
  • “Ctrl”-“X”

This gets the status of the repository.

git status

This swaps line one and six in storelist.txt and saves the file.

Open the storelist.txt file.

  • Swap the first row and the sixth row.
  • Click "Ctrl"+"S" to save.

This gets the status of the repository.

git status

This commits the change to the repository, setting the commit comment from the command line.

git commit -a -m "Reordered list"

This gets the status of the repository.

git status

This swaps the third and fourth lines in storelist.txt and saves the file.

Swap the third and fourth rows in storelist.txt and save the file.

This commits the change to the repository, setting the commit comment from the command line.


git commit -a -m "Another reorder"

This gets the status of the repository.

git status

Terminal 1
Terminal

Click to Connect...

Quiz Yourself on Git Essentials

Git Kata 2: Branches